//
//  BHBrowserImageView.m
//  Behing
//
//  Created by Lj on 2017/10/23.
//  Copyright © 2017年 lj. All rights reserved.
//
#define MaxSCale 3.0  //最大缩放比例
#define MinScale 1.0  //最小缩放比例

#import "BHBrowserImageView.h"
#import "UIImageView+WebCache.h"
#import <ImageIO/ImageIO.h>
#import "BHImageModel.h"

@interface BHBrowserImageView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImage *currentImage;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, assign) CGFloat total;


@end

@implementation BHBrowserImageView

#pragma mark - Lazy
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    self.delegate = self;
    self.minimumZoomScale = MinScale;
    self.maximumZoomScale = MaxSCale;
    self.backgroundColor = [UIColor blackColor];
    
    self.imageView = [[UIImageView alloc]init];
    [self addSubview:self.imageView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
    [self addGestureRecognizer:tapGesture];
    
    UITapGestureRecognizer *doubleGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGestureClick:)];
    doubleGesture.numberOfTapsRequired = 2;
    [self addGestureRecognizer:doubleGesture];
    
    //捏合事件
    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]init];
    [pinchRecognizer addTarget:self action:@selector(pinchClick:)];
    [self addGestureRecognizer:pinchRecognizer];
    
    [tapGesture requireGestureRecognizerToFail:doubleGesture];
}

#pragma mark - 自适应图片宽高比
- (void)_layoutImageView {
    CGRect imageFrame;
    if (_currentImage.size.width > self.bounds.size.width || _currentImage.size.height > self.bounds.size.height) {
        CGFloat imageRatio = _currentImage.size.width/_currentImage.size.height;
        CGFloat photoRatio = self.bounds.size.width/self.bounds.size.height;
        
        if (imageRatio > photoRatio) {
            imageFrame.size = CGSizeMake(self.bounds.size.width, self.bounds.size.width/_currentImage.size.width*_currentImage.size.height);
            imageFrame.origin.x = 0;
            imageFrame.origin.y = (self.bounds.size.height-imageFrame.size.height)/2.0;
        }else {
            imageFrame.size = CGSizeMake(self.bounds.size.height/_currentImage.size.height*_currentImage.size.width, self.bounds.size.height);
            imageFrame.origin.x = (self.bounds.size.width-imageFrame.size.width)/2.0;
            imageFrame.origin.y = 0;
        }
    }else {
        imageFrame.size = _currentImage.size;
        imageFrame.origin.x = (self.bounds.size.width-_currentImage.size.width)/2.0;
        imageFrame.origin.y = (self.bounds.size.height-_currentImage.size.height)/2.0;
    }
    _imageView.frame = imageFrame;
    _imageView.image = _currentImage;
    if (self.imageArray.count > 1) {
        [_imageView setAnimationImages:self.imageArray];
        [_imageView setAnimationDuration:self.total];
        [_imageView setAnimationRepeatCount:LONG_MAX];
        [_imageView startAnimating];
    }
}

#pragma mark - Set
- (void)setShowImage:(id)showImage {
    if (self.imageArray.count > 1) {
        self.total = 0;
        [self.imageView stopAnimating];
        [self.imageArray removeAllObjects];
    }
    _showImage = showImage;
    self.imageView.image = nil;
    
    if ([BHSettingUtil dataAndStringIsNull:showImage]) {
        self.currentImage = [UIImage imageNamed:@"icon_placeholder"];
    }else if ([showImage isKindOfClass:[UIImage class]]) {
        self.currentImage = (UIImage *)showImage;
    }else if ([showImage isKindOfClass:[NSString class]]) {
        if([(NSString *)showImage hasPrefix:@"http://"]) {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)showImage] placeholderImage:[UIImage imageNamed:@"icon_placeholder"]];
            self.currentImage = self.imageView.image;
        }else {
            self.currentImage = [UIImage imageNamed:(NSString *)showImage];
        }
    }else if ([showImage isKindOfClass:[BHImageModel class]]) {
        BHImageModel *model = (BHImageModel *)showImage;
        if([model.image hasPrefix:@"http://"]) {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"icon_placeholder"]];
            self.currentImage = self.imageView.image;
        }else {
            self.currentImage = [UIImage imageNamed:model.image];
        }
    }
    [self _layoutImageView];
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    CGFloat offsetX = (self.bounds.size.width > self.contentSize.width)?(self.bounds.size.width - self.contentSize.width) * 0.5 : 0.0;
    CGFloat offsetY = (self.bounds.size.height > self.contentSize.height) ? (self.bounds.size.height - self.contentSize.height) * 0.5 : 0.0;
    self.imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);
}

#pragma mark - 手势
//单击
- (void)tapGestureClick:(UITapGestureRecognizer *)gestureRecognizer {
    if (self.imageDelegate && [self.imageDelegate respondsToSelector:@selector(deleteSelectImageView:deleteImage:)]) {
        [self.imageDelegate deleteSelectImageView:self deleteImage:self.currentImage];
    }
}

//双击
- (void)doubleGestureClick:(UITapGestureRecognizer *)gestureRecognizer {
    if (self.zoomScale > MinScale) {
        [self setZoomScale:MinScale animated:YES];
    } else {
        CGPoint touchPoint = [gestureRecognizer locationInView:self.imageView];
        CGFloat newZoomScale = self.maximumZoomScale;
        CGFloat xsize = self.frame.size.width / newZoomScale;
        CGFloat ysize = self.frame.size.height / newZoomScale;
        [self zoomToRect:CGRectMake(touchPoint.x - xsize / 2, touchPoint.y - ysize / 2, xsize, ysize) animated:YES];
    }
}

//捏合
- (void)pinchClick:(UIPinchGestureRecognizer *)gestureRecognizer {
}
@end
